package edu.hubu.book.controller;

import edu.hubu.book.domain.BookClass;
import edu.hubu.book.domain.BookClassResultInfo;
import edu.hubu.book.service.impl.BookClassServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类web
 */
@Controller
@RequestMapping("/book")
public class BookClassController {

    @Autowired
    private BookClassServiceImpl bookService;


    /**
     * 查询所有分类
     * @return
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ResponseBody
    public List<BookClass> findAll(){
        return bookService.findAll();
    }

    /**
     * 增加一级分类
     * @param bookClass
     */
    @RequestMapping(value = "/addFirstClass",method = RequestMethod.POST)
    @ResponseBody
    public BookClassResultInfo addFirstClass(BookClass bookClass){
        if (bookClass.getDescription()==null) {
            bookClass.setDescription("暂无描述");
        }
           int flag= bookService.addFirstClass(bookClass);
        if (flag==0){
            return new BookClassResultInfo(0,"添加一级分类失败！");
        }else {
            return new BookClassResultInfo(1,"添加一级分类成功！");
        }
    }

    /**
     * 增加二级分类
     * @param bookClass
     */
    @RequestMapping(value = "/addSecondClass",method = RequestMethod.POST)
    public BookClassResultInfo addSecondClass(BookClass bookClass){
        boolean ifFirstExist = bookService.findById(bookClass.getFirstId());
        if (!ifFirstExist){
            return new BookClassResultInfo(0,"该二级目录对应的一级目录不存在！");
        }else {
            int flag=bookService.addSecondClass(bookClass);
            if (flag==0){
                return new BookClassResultInfo(0,"添加二级目录失败");
            }else {
                return new BookClassResultInfo(1,"添加二级目录成功");
            }
        }
    }

    /**
     * 根据sid更新一级分类
     * @param bookClass
     * @return
     */
    @RequestMapping(value = "/updateFirstClass",method = RequestMethod.POST)
    @ResponseBody
    public BookClassResultInfo updateFirstClass(BookClass bookClass) {
            //修改一级分类
        boolean ifFirstExist = bookService.findById(bookClass.getSid());
            if (!ifFirstExist) {
                return new BookClassResultInfo(0,"对应一级目录不存在！");
            } else {
                if (bookClass.getDescription() == null) {
                    bookClass.setDescription("暂无描述");
                }
                int flag=bookService.updateFirstClass(bookClass);
                if (flag==0){
                    return new BookClassResultInfo(0,"修改一级目录失败");
                }else {
                    return new BookClassResultInfo(1,"修改一级目录成功");
                }
            }
    }

    /**
     * 根据ssid更新二级分类
     * @param bookClass
     * @return
     */
    @RequestMapping(value = "/updateSecondClass",method = RequestMethod.POST)
    @ResponseBody
    public BookClassResultInfo updateSecondClass(BookClass bookClass){
        boolean ifFirstExist = bookService.findById(bookClass.getFirstId());
        if (!ifFirstExist) {
            return new BookClassResultInfo(0,"对应一级分类不存在！");
        }else {
            int flag=bookService.updateSecondClass(bookClass);
            if (flag==0){
                return new BookClassResultInfo(0,"修改二级目录失败");
            }else {
                return new BookClassResultInfo(1,"修改二级目录成功");
            }

        }
    }

    /**
     * 根据名称删除一级分类（对应二级分类全部删除）
     * @param bookClass
     */
    @RequestMapping(value = "/deleteFirstClass",method = RequestMethod.POST)
    @ResponseBody
    public BookClassResultInfo deleteFirstClass(BookClass bookClass){
            boolean ifFirstExist = bookService.findById(bookClass.getSid());
            if (!ifFirstExist) {
                System.out.println("该一级分类不存在！");
                return new BookClassResultInfo(0,"对应一级分类不存在！");
            }
        //根据分类名称删除，一级分类所属二级分类随之删除
        int flag=bookService.deleteFirstClass(bookClass);
            if (flag==0){
                return new BookClassResultInfo(0,"删除一级分类失败！");
            }else {
                return new BookClassResultInfo(1,"删除一级分类成功！");
            }
        }

    /**
     * 根据名称删除二级分类
     * @param bookClass
     * @return
     */
    @RequestMapping(value = "/deleteSecondClass",method = RequestMethod.POST)
    @ResponseBody
    public BookClassResultInfo deleteSecondClass(BookClass bookClass){
        boolean ifFirstExist = bookService.findById(bookClass.getFirstId());
        if (!ifFirstExist) {
            return new BookClassResultInfo(0,"对应一级分类不存在！");
        }
        //根据分类名称删除
        int flag=bookService.deleteSecondClass(bookClass);
        if (flag==0){
            return new BookClassResultInfo(0,"删除二级分类失败！");
        }else {
            return new BookClassResultInfo(1,"删除二级分类成功！");
        }
    }
}

